/**
 *  Copyright 2010 ShenYang voole Co. Ltd.
 *  All right reserved. 
 */
package cn.piaoyi.dao.xml;

import cn.piaoyi.bean.City;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.dao.CityDAO;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
 * 功能描述:省操作实现
 *
 * 创建日期 Apr 10, 2010
 *
 * @author zhangbaochao
 *
 * @version 1.0
 *
 */
public class CityXMLDAO implements CityDAO {

	/**
	 * @param id
	 * @return City
	 * 根据ID获取City
	 */
	public City findCityById(long id){
		try {
			List l = new ArrayList();
			File file = new File(Constant.ROOTPATH + Constant.ARTAXMLPATH);
			FileInputStream in = new FileInputStream(file);
			Reader reader = new InputStreamReader(in,"utf-8");
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(reader);
			List list = document.selectNodes("/Area/Province/City");
			Iterator iter = list.iterator();
			long cityId = 0;
			long provinceId = 0;
			String cityName = "";
			while(iter.hasNext()){
				Element cityElement = (Element)iter.next();
				Attribute idElement = (Attribute) cityElement.attribute("ID");
				Attribute nameElement = (Attribute) cityElement.attribute("Name");
				Element provinceElement = cityElement.getParent();
				Attribute pidElement = (Attribute) provinceElement.attribute("ID");
				cityId = Long.parseLong(idElement.getValue());				
					if(cityId == id){
					City c = new City();
					c.setId(Long.parseLong(idElement.getValue()));
					c.setName(nameElement.getValue());
					c.setProvinceId(Long.parseLong(pidElement.getValue()));
					return c;
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}
	  
	/**
	 * @param provinceId
	 * @return List
	 * 根据provinceId获取City列表
	 */
	public List findCitysByProvinceId(long provinceId){
		List l = new ArrayList();
		try {		
			File file = new File(Constant.ROOTPATH + Constant.ARTAXMLPATH);
			FileInputStream in = new FileInputStream(file);
			Reader reader = new InputStreamReader(in,"utf-8");
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(reader);
			List list = document.selectNodes("/Area/Province");
			Iterator iter = list.iterator();
			while(iter.hasNext()){
				Element provinceElement = (Element)iter.next();
				Attribute pidElement = (Attribute) provinceElement.attribute("ID");
				long pid = Long.parseLong(pidElement.getValue());
				if(pid == provinceId){
					List cityList = provinceElement.selectNodes("City");
					iter = cityList.iterator();
					long cityId = 0;
					while(iter.hasNext()){
						Element cityElement = (Element)iter.next();
						Attribute idElement = (Attribute) cityElement.attribute("ID");
						Attribute nameElement = (Attribute) cityElement.attribute("Name");
						cityId = Long.parseLong(idElement.getValue());				
						City c = new City();
						c.setId(Long.parseLong(idElement.getValue()));
						c.setName(nameElement.getValue());
						c.setProvinceId(Long.parseLong(pidElement.getValue()));
						l.add(c);
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}finally{
			return l;
		}
	  }
	
	/**
	 * @param provinceId
	 * @param name
	 * @return List
	 * 根据provinceId获取City列表
	 */
	public City findCitysByProvinceIdName(long provinceId,String name){
		try {		
			File file = new File(Constant.ROOTPATH + Constant.ARTAXMLPATH);
			FileInputStream in = new FileInputStream(file);
			Reader reader = new InputStreamReader(in,"utf-8");
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(reader);
			List list = document.selectNodes("/Area/Province");
			Iterator iter = list.iterator();
			while(iter.hasNext()){
				Element provinceElement = (Element)iter.next();
				Attribute pidElement = (Attribute) provinceElement.attribute("ID");
				long pid = Long.parseLong(pidElement.getValue());
				if(pid == provinceId){
					List cityList = provinceElement.selectNodes("City");
					iter = cityList.iterator();
					long cityId = 0;
					String cityName = "";
					while(iter.hasNext()){
						Element cityElement = (Element)iter.next();
						Attribute idElement = (Attribute) cityElement.attribute("ID");
						Attribute nameElement = (Attribute) cityElement.attribute("Name");
						cityName = nameElement.getValue();
						if(cityName.equalsIgnoreCase(name)){
							cityId = Long.parseLong(idElement.getValue());				
							City c = new City();
							c.setId(Long.parseLong(idElement.getValue()));
							c.setName(nameElement.getValue());
							c.setProvinceId(Long.parseLong(pidElement.getValue()));
							return c;
						}
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}
	 
	/**
	 * @return List
	 * 获取所有City列表
	 */
	public List findCityAll(){
		List l = new ArrayList();
		try {			
			File file = new File(Constant.ROOTPATH + Constant.ARTAXMLPATH);
			FileInputStream in = new FileInputStream(file);
			Reader reader = new InputStreamReader(in,"utf-8");
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(reader);
			List list = document.selectNodes("/Area/Province/City");
			Iterator iter = list.iterator();
			while(iter.hasNext()){
				Element cityElement = (Element)iter.next();
				Attribute idElement = (Attribute) cityElement.attribute("ID");
				Attribute nameElement = (Attribute) cityElement.attribute("Name");
				Element provinceElement = cityElement.getParent();
				Attribute pidElement = (Attribute) provinceElement.attribute("ID");			
				City c = new City();
				c.setId(Long.parseLong(idElement.getValue()));
				c.setName(nameElement.getValue());
				c.setProvinceId(Long.parseLong(pidElement.getValue()));
				l.add(c);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return l;
	}
}
